package com.rex.saas.controller;

import com.rex.saas.bean.BaseResponse;
import com.rex.saas.bean.DeviceBasicData;
import com.rex.saas.bean.ResultCode;
import com.rex.saas.constants.Constants;
import com.rex.saas.db.entity.SaasDeviceInfo;
import com.rex.saas.db.entity.SaasSpaceInfo;
import com.rex.saas.db.entity.SaasStrategyTask;
import com.rex.saas.service.*;
import com.rex.saas.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@Slf4j
public class BaseController {
    @Autowired
    protected DeviceService deviceService;

    @Autowired
    protected GatewayService gatewayService;

    @Autowired
    protected SpaceService spaceService;

    @Autowired
    protected GroupService groupService;

    @Autowired
    protected RexolarApiClient rexolarApiClient;

    protected HashMap<String, DeviceBasicData> deviceMap = new HashMap<>();


    protected String getCustomerDevTenantToken(String token, String deviceName) {
        List<String> roles = JwtUtils.getRole( token);
        if(roles.contains(Constants.TENANT_ADMIN)){
            return token;
        }
        String customerId = JwtUtils.getCustomerId(token);
        SaasDeviceInfo deviceInfo = deviceService.getDeviceByName(deviceName, JwtUtils.getTenantId(token));
        if(deviceInfo == null || deviceInfo.getCustomerId()==null || !customerId.equals(deviceInfo.getCustomerId())){
            log.error("getCustomerTenantToken error, deviceName:{}", deviceName);
            return null;
        }
        return rexolarApiClient.getTenantToken(JwtUtils.getTenantId( token));
    }

    protected String getCustomerSpaceTenantToken(String token, Long spaceId) {
        List<String> roles = JwtUtils.getRole( token);
        if(roles.contains(Constants.TENANT_ADMIN)){
            return token;
        }
        String customerId = JwtUtils.getCustomerId(token);
        SaasSpaceInfo saasSpaceInfo = spaceService.getSpaceById(spaceId);
        if(saasSpaceInfo == null || saasSpaceInfo.getCustomerId()==null || !customerId.equals(saasSpaceInfo.getCustomerId())){
            log.error("getCustomerSpaceTenantToken error, saasSpaceInfo:{}", saasSpaceInfo);
            return null;
        }
        return rexolarApiClient.getTenantToken(JwtUtils.getTenantId( token));
    }

    protected BaseResponse validateDeviceName(String deviceName, String token){
        String tenantId = JwtUtils.getTenantId(token);
        String customerId = JwtUtils.getCustomerId(token);
        SaasDeviceInfo saasDeviceInfo = deviceService.getDeviceByName(deviceName, tenantId);
        if(saasDeviceInfo==null){
            log.error("validateDeviceName getDeviceByName error, deviceName:{}", deviceName);
            return BaseResponse.error("设备不存在", ResultCode.PARAM_ERROR);
        }

        List<String> roles = JwtUtils.getRole(token);
        if(!roles.contains(Constants.TENANT_ADMIN) && !customerId.equals(saasDeviceInfo.getCustomerId())){
            log.error("validateDeviceName space customerId not match, customerId:{}, saasSpaceInfo:{}", tenantId, saasDeviceInfo);
            return BaseResponse.error("无权限", ResultCode.PARAM_ERROR);
        }
        return BaseResponse.ok(saasDeviceInfo.getId());
    }

    protected BaseResponse validateSpace(Long spaceId, String token){
        String tenantId = JwtUtils.getTenantId(token);
        String customerId = JwtUtils.getCustomerId(token);
        SaasSpaceInfo space = spaceService.getSpaceById(spaceId);
        if(space==null){
            log.error("validateSpace space is null,spaceInfoId:{}", spaceId);
            return BaseResponse.error("空间不存在", ResultCode.PARAM_ERROR);
        }
        if(!tenantId.equals(space.getTenantId())){
            log.error("validateSpace space tenantId not match, tenantId:{}, saasSpaceInfo:{}", tenantId, space);
            return BaseResponse.error("非法空间", ResultCode.PARAM_ERROR);
        }

        List<String> roles = JwtUtils.getRole(token);
        if(!roles.contains(Constants.TENANT_ADMIN) && !customerId.equals(space.getCustomerId())){
            log.error("validateSpace space customerId not match, customerId:{}, saasSpaceInfo:{}", tenantId, space);
            return BaseResponse.error("非法空间", ResultCode.PARAM_ERROR);
        }
        return BaseResponse.ok(space);
    }

}
